<?php defined("MAIN_DIR") or die("No direct access");

$cart_id = UV::readInt($route->getPath(5));

$edit_error = false;
$cur_error = false;
if (isset($_POST['action']) && $_POST['action'] == "edit")
{
	if (!isset($_POST['RUB']) && !isset($_POST['USD']) && !isset($_POST['EUR']))
		$cur_error = true;
	$bank = filter_var($_POST['bank'],FILTER_SANITIZE_MAGIC_QUOTES);
	$regionally = $route->POST('regionally') ? 1 : 0;
	if ($db->getCell("select `parent` from `bank` where `bank_id` = '$bank' limit 1")) {
		$regionally = 0;
	}
	$name = trim(filter_var($_POST['name'],FILTER_SANITIZE_MAGIC_QUOTES));
	$cart_type = trim(filter_var($_POST['cart_type'],FILTER_SANITIZE_MAGIC_QUOTES));
	$own_funds = trim(filter_var($_POST['own_funds'],FILTER_SANITIZE_MAGIC_QUOTES));
	$release_cost = trim(filter_var($_POST['release_cost'],FILTER_SANITIZE_MAGIC_QUOTES));
	$yearly_service = trim(filter_var($_POST['yearly_service'],FILTER_SANITIZE_MAGIC_QUOTES));
	$initial_fee = trim(filter_var($_POST['initial_fee'],FILTER_SANITIZE_MAGIC_QUOTES));
	$cashless_payments = trim(filter_var($_POST['cashless_payments'],FILTER_SANITIZE_MAGIC_QUOTES));
	$taking_own_bank = trim(filter_var($_POST['taking_own_bank'],FILTER_SANITIZE_MAGIC_QUOTES));
	$taking_other_bank = trim(filter_var($_POST['taking_other_bank'],FILTER_SANITIZE_MAGIC_QUOTES));
	$pvn_other_bank = trim(filter_var($_POST['pvn_other_bank'],FILTER_SANITIZE_MAGIC_QUOTES));
	$documents = trim(filter_var($_POST['documents'],FILTER_SANITIZE_MAGIC_QUOTES));
	$repayment_mode = trim(filter_var($_POST['repayment_mode'],FILTER_SANITIZE_MAGIC_QUOTES));
	$accrual_balances = trim(filter_var($_POST['accrual_balances'],FILTER_SANITIZE_MAGIC_QUOTES));
	$info = trim(filter_var($_POST['info'],FILTER_SANITIZE_MAGIC_QUOTES));
	$edit_query = "update `cart` set `bank_id`='$bank',`regionally`='$regionally',`name`='$name',`cart_type`='$cart_type',`own_funds`='$own_funds',`release_cost`='$release_cost',`yearly_service`='$yearly_service',`initial_fee`='$initial_fee',`cashless_payments`='$cashless_payments',`taking_own_bank`='$taking_own_bank',`taking_other_bank`='$taking_other_bank',`pvn_other_bank`='$pvn_other_bank',`documents`='$documents',`repayment_mode`='$repayment_mode',`accrual_balances`='$accrual_balances',`info`='$info' where `cart_id` = '$_POST[cart_id]' limit 1";
	if (!$cur_error)
	{
		if (mysql_query($edit_query))
		{
			if (isset($_POST['RUB']))
			{
				$rub_res = mysql_query("select * from `credit_currency` where `credit_id` = '$_POST[cart_id]' and `type` = '4' and `currency` = 'RUB'");
				if (mysql_num_rows($rub_res) > 0)
					$rub_query = "update `credit_currency` set `sum_low`='$_POST[RUB_sum_low]',`sum_high`='$_POST[RUB_sum_high]',`period_low`='$_POST[RUB_period_low]',`period_high`='$_POST[RUB_period_high]',`rate_low`='$_POST[RUB_rate_low]',`rate_high`='$_POST[RUB_rate_high]' where `credit_id` = '$_POST[cart_id]' and `type` = '4' and `currency` = 'RUB' limit 1";
				else
					$rub_query = "insert into `credit_currency` (`credit_id`,`type`,`currency`,`sum_low`,`sum_high`,`period_low`,`period_high`,`rate_low`,`rate_high`) values ('$_POST[cart_id]','4','RUB','$_POST[RUB_sum_low]','$_POST[RUB_sum_high]','$_POST[RUB_period_low]','$_POST[RUB_period_high]','$_POST[RUB_rate_low]','$_POST[RUB_rate_high]')";
				mysql_query($rub_query);
			}
			else
				mysql_query("delete from `credit_currency` where `credit_id` = '$_POST[cart_id]' and `type` = '4' and `currency` = 'RUB' limit 1");
			if (isset($_POST['USD']))
			{
				$usd_res = mysql_query("select * from `credit_currency` where `credit_id` = '$_POST[cart_id]' and `type` = '4' and `currency` = 'USD'");
				if (mysql_num_rows($usd_res) > 0)
					$usd_query = "update `credit_currency` set `sum_low`='$_POST[USD_sum_low]',`sum_high`='$_POST[USD_sum_high]',`period_low`='$_POST[USD_period_low]',`period_high`='$_POST[USD_period_high]',`rate_low`='$_POST[USD_rate_low]',`rate_high`='$_POST[USD_rate_high]' where `credit_id` = '$_POST[cart_id]' and `type` = '4' and `currency` = 'USD' limit 1";
				else
					$usd_query = "insert into `credit_currency` (`credit_id`,`type`,`currency`,`sum_low`,`sum_high`,`period_low`,`period_high`,`rate_low`,`rate_high`) values ('$_POST[cart_id]','4','USD','$_POST[USD_sum_low]','$_POST[USD_sum_high]','$_POST[USD_period_low]','$_POST[USD_period_high]','$_POST[USD_rate_low]','$_POST[USD_rate_high]')";
				mysql_query($usd_query);
			}
			else
				mysql_query("delete from `credit_currency` where `credit_id` = '$_POST[cart_id]' and `type` = '4' and `currency` = 'USD' limit 1");
			if (isset($_POST['EUR']))
			{
				$eur_res = mysql_query("select * from `credit_currency` where `credit_id` = '$_POST[cart_id]' and `type` = '4' and `currency` = 'EUR'");
				if (mysql_num_rows($eur_res) > 0)
					$eur_query = "update `credit_currency` set `sum_low`='$_POST[EUR_sum_low]',`sum_high`='$_POST[EUR_sum_high]',`period_low`='$_POST[EUR_period_low]',`period_high`='$_POST[EUR_period_high]',`rate_low`='$_POST[EUR_rate_low]',`rate_high`='$_POST[EUR_rate_high]' where `credit_id` = '$_POST[cart_id]' and `type` = '4' and `currency` = 'EUR' limit 1";
				else
					$eur_query = "insert into `credit_currency` (`credit_id`,`type`,`currency`,`sum_low`,`sum_high`,`period_low`,`period_high`,`rate_low`,`rate_high`) values ('$_POST[cart_id]','4','EUR','$_POST[EUR_sum_low]','$_POST[EUR_sum_high]','$_POST[EUR_period_low]','$_POST[EUR_period_high]','$_POST[EUR_rate_low]','$_POST[EUR_rate_high]')";
				mysql_query($eur_query);
			}
			else
				mysql_query("delete from `credit_currency` where `credit_id` = '$_POST[cart_id]' and `type` = '4' and `currency` = 'EUR' limit 1");
		}
		else
			$add_error = true;
	}
	else
		$add_error = true;
}
if ($cart_id)
{
	$cart_res = mysql_query("select * from `cart` where `cart_id` = '$cart_id' limit 1");
	if (mysql_num_rows($cart_res) == 0) {
		to("/admin/credits/carts/");
	}
	$cart_row = mysql_fetch_array($cart_res);
	
	$city_path = $db->getCell("select `C`.`path` from `city` as `C`,`bank` as `B` where `B`.`bank_id` = '$cart_row[bank_id]' and `B`.`city_id` = `C`.`city_id` limit 1");
}

require_once ADMIN.HEADFILE;
?>
<script type="text/javascript" src="/files/js/admin/currency.js"></script>
<script type="text/javascript">
$(document).ready(function(){
	$("#bank").change(function(){
		if ($("#bank option:selected").hasClass("branch")) {
			$("#regionally").attr("disabled","disabled").removeAttr("checked");
		} else {
			$("#regionally").removeAttr("disabled");
		}
	})
})
</script>
<div align="left">
<h1>Редактирование кредита (Кредитные карты)</h1>
<hr /><?php
if (isset($_POST['action']) && $_POST['action'] == "edit" && !$edit_error)
	echo "<p style='color:green'>Успешно отредактировано</p>";
elseif ($cur_error)
	echo "<p style='color:red'>Необходимо указать данные хотя бы для одной валюты</p>";
elseif ($edit_error)
	echo "<p style='color:red'>Не удалось отредактировать</p>";
?><form method='POST'>
<input type='hidden' name='action' value='edit' />
<input type='hidden' name='cart_id' value='<?php echo $cart_row['cart_id']; ?>' />
<?php
$cur_res = mysql_query("select * from `credit_currency` where `credit_id` = '$cart_id' and `type` = '4';");
require_once ACREDITS."edit_credit_currency.php";
?>
<table cellpadding='5' cellspacing='0'>
	<tr>
		<td width="200px">Ссылка на сайте</td>
		<td width="500px">
			<a href='<?php echo "/$city_path/credits/cart/".$cart_row['cart_id'];?>' target="_blank"><?php echo $cart_row['name'];?></a>
		</td>
	</tr>
	<tr>
		<td>ID</td>
		<td><input style="width:200px" type='text' name='cart_id' value='<?php echo $cart_row['cart_id'];?>' disabled /></td>
	</tr>
	<tr>
		<td>Название</td>
		<td><input style='width:100%' type='text' name='name' value='<?php echo $cart_row['name'];?>' /></td>
	</tr>
	<tr>
		<td>Банк</td>
		<td>
			<select style="width:200px" name='bank' id='bank'>
				<?php
				$bankslist = $db->getTable("select `bank_id`,`name` from `bank` where `parent` = '0' order by `name`");
				echo "<option disabled='disabled' style='color:gray'>———————————————</option>
						<option disabled='disabled' style='color:gray'>Головные офисы</option>
						<option disabled='disabled' style='color:gray'>———————————————</option>";
				if ($bankslist) {
					
					foreach ($bankslist as $bank) {
						
						echo "<option value='$bank[bank_id]'";
						if ($bank['bank_id'] == $cart_row['bank_id'])
							echo " selected='selected'";
						echo ">$bank[name]</option>";
					}
				} else {
					echo "<option disabled='disabled' style='color:gray'>В данном городе нет головных офисов</option>";
				}
				$branches = $db->getTable("select `bank_id`,`name` from `bank` where `parent` != '0' and `city_id` = '$_SESSION[city_admin]' order by `name`");
				echo "<option disabled='disabled' style='color:gray'>———————————————</option>
						<option disabled='disabled' style='color:gray'>Филиалы</option>
						<option disabled='disabled' style='color:gray'>———————————————</option>";
				$regional = false;
				if ($branches) {
					
					foreach ($branches as $branch) {
						
						echo "<option class='branch' value='$branch[bank_id]'";
						if ($branch['bank_id'] == $cart_row['bank_id']) {
							echo " selected='selected'";
							$regional = true;
						}
						echo ">$branch[name]</option>";
					}
				} else {
					echo "<option disabled='disabled' style='color:gray'>В данном городе нет филиалов</option>";
				}
				?>
			</select>
		</td>
	</tr>
	<tr>
		<td>Межрегионально</td>
		<td colspan='2' align="left">
			<input type='checkbox' name='regionally' id='regionally' <?php
				if ($cart_row['regionally']) {
					echo "checked='checked'";
				}
				if ($regional) {
					echo "disabled='disabled'";
				}
			?> />
		</td>
	</tr>
	<tr>
		<td>Тип карты</td>
		<td>
			<input style='width:100%' type='text' name='cart_type' value='<?php echo $cart_row['cart_type']; ?>' />
		</td>
	</tr>
	<tr>
		<td>Использование собственных средств</td>
		<td>
			<input style='width:100%' type='text' name='own_funds' value='<?php echo $cart_row['own_funds']; ?>' />
		</td>
	</tr>
	<tr>
		<td>Стоимость выпуска</td>
		<td>
			<input style='width:100%' type='text' name='release_cost' value='<?php echo $cart_row['release_cost']; ?>' />
		</td>
	</tr>
	<tr>
		<td>Годовое обслуживание</td>
		<td>
			<input style='width:100%' type='text' name='yearly_service' value='<?php echo $cart_row['yearly_service']; ?>' />
		</td>
	</tr>
	<tr>
		<td>Первоначальный взнос (%)</td>
		<td>
			<input style='width:200px' type='text' name='initial_fee' value='<?php echo $cart_row['initial_fee']; ?>' />
		</td>
	</tr>
	<tr>
		<td>Безналичные платежи</td>
		<td>
			<input style='width:100%' type='text' name='cashless_payments' value='<?php echo $cart_row['cashless_payments']; ?>' />
		</td>
	</tr>
	<tr>
		<td>Снятие наличных в банкоматах банка</td>
		<td>
			<input style='width:100%' type='text' name='taking_own_bank' value='<?php echo $cart_row['taking_own_bank']; ?>' />
		</td>
	</tr>
	<tr>
		<td>Снятие наличных в банкоматах других банков</td>
		<td>
			<input style='width:100%' type='text' name='taking_other_bank' value='<?php echo $cart_row['taking_other_bank']; ?>' />
		</td>
	</tr>
	<tr>
		<td>Снятие наличных в ПВН других банков</td>
		<td>
			<input style='width:100%' type='text' name='pvn_other_bank' value='<?php echo $cart_row['pvn_other_bank']; ?>' />
		</td>
	</tr>
	<tr>
		<td>Документы</td>
		<td>
			<input style='width:100%' type='text' name='documents' value='<?php echo $cart_row['documents']; ?>' />
			<br /><span style='color:gray;font-size:8pt'>Список документов через запятую</span>
		</td>
	</tr>
	<tr>
		<td>Погашение кредита</td>
		<td>
			<input style='width:100%' type='text' name='repayment_mode' value='<?php echo $cart_row['repayment_mode']; ?>' />
		</td>
	</tr>
	<tr>
		<td>Начисление процентов на остаток средств на счёте (%)</td>
		<td>
			<input style='width:200px' type='text' name='accrual_balances' value='<?php echo $cart_row['accrual_balances']; ?>' />
		</td>
	</tr>
	<tr valign='top'>
		<td>Инфо</td>
		<td><textarea style='width:100%' rows='5' name='info' id='info'><?php echo $cart_row['info']; ?></textarea></td>
	</tr>
	<tr valign='top'>
		<td colspan='2'>
			<p><input type='button' id="insert_city" value='Вставить' /> - Вставить название города<br />
			<input type='button' id="insert_city_rod" value='Вставить' /> - Вставить название города в родительном падеже<br />
			<input type='button' id="insert_city_pred" value='Вставить' /> - Вставить название города в предложном падеже<br />
			</p>
		</td>
	</tr>
	<tr valign='top'>
		<td colspan='2' align="right"><button style="width:100px;height:30px" type="submit">Сохранить</button></td>
	</tr>
</table>
</form>
<br /><hr /><br />
</div>

<?php require_once ADMIN.FOOTERFILE; ?>